package com.cskaoyan.service.mall;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.goods.Comment;
import com.cskaoyan.bean.mall.order.*;
import com.cskaoyan.mapper.goods.CommentMapper;
import com.cskaoyan.mapper.mall.OrderMapper;
import com.cskaoyan.utils.Constant;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Repository
@Transactional
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;
    @Resource
    CommentMapper commentMapper;

    @Override
    public List<Order> queryOrderList(BaseParam baseParam, OrderBO orderBO) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (orderBO.getUserId() != null) criteria.andUserIdEqualTo(orderBO.getUserId());
        if (orderBO.getOrderSn() != null) criteria.andOrderSnLike("%" + orderBO.getOrderSn() + "%");
        if (orderBO.getStart() != null && orderBO.getEnd() != null)
            criteria.andAddTimeBetween(orderBO.getStart(), orderBO.getEnd());
        if (orderBO.getOrderStatusArray() != null)
            criteria.andOrderStatusEqualTo(Short.valueOf(orderBO.getOrderStatusArray().toString()));
        return orderMapper.selectByExample(orderExample);
    }

    @Override
    public List<Channel> queryChannel() {
        List<Channel> channels = orderMapper.queryChannel();
        return channels;
    }

    @Override
    public OrderDetail queryOrderDetail(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        List<OrderGoods> orderGoods = orderMapper.queryOrderGoods(order.getId());
        User user = orderMapper.queryUser(order.getUserId());
        return new OrderDetail(orderGoods, user, order);
    }

    @Override
    public int deleteOrder(Integer id) {
        Order order = new Order();
        order.setDeleted(true);
        order.setId(id);
        int code = orderMapper.updateByPrimaryKeySelective(order);
        int i = orderMapper.updateOrderGoods(true, id);
        if (code == 1) return Constant.SUCCESS;
        return Constant.FAIL;
    }

    @Override
    public int refundMoney(RefundMoney refundMoney) {
        Order order = new Order();
        Order originOrder = orderMapper.selectByPrimaryKey(refundMoney.getOrderId());
        BigDecimal Money = refundMoney.getRefundMoney();
        BigDecimal actualPrice = originOrder.getActualPrice();
        //扣除的钱不能小于实付价格
        if (actualPrice.subtract(Money).compareTo(new BigDecimal(0)) > 0) return Constant.FAIL;
        order.setRefundAmount(Money);
        order.setId(refundMoney.getOrderId());
        order.setOrderStatus((short) 203);
        order.setAftersaleStatus((short) 3);
        order.setRefundTime(new Date());
        int i = orderMapper.updateByPrimaryKeySelective(order);
        if (i == 1) return Constant.SUCCESS;
        return Constant.FAIL;
    }

    @Override
    public int shipUpdateOrder(ShipBO shipBO) {
        Order order = new Order();
        order.setShipSn(shipBO.getShipSn());
        order.setOrderStatus((short) 301);
        order.setId(shipBO.getOrderId());
        int i = orderMapper.updateByPrimaryKeySelective(order);
        if (i == 1) return Constant.SUCCESS;
        return Constant.FAIL;
    }

    @Override
    public int replyComment(ReplyComment replyComment) {
        Comment comment = new Comment();
        comment.setId(replyComment.getCommentId());
        comment.setAdminContent(replyComment.getContent());
        int count = commentMapper.updateByPrimaryKeySelective(comment);
        if (count == 1) return Constant.SUCCESS;
        return Constant.FAIL;
    }
}
